/**
 * https://leetcode.cn/problems/sum-of-digit-differences-of-all-pairs/submissions/559809810/?envType=daily-question&envId=2024-08-30
 */

class Solution {
public:
    typedef long long ll;
    long long sumDigitDifferences(vector<int>& nums) {
        map<int,int> mp;
        ll k=0;
        while(nums[0]>0){
            ll sum=0;
            for(int i=0;i<nums.size();++i){
                mp[nums[i]%10]++;
                nums[i]/=10;
                sum++;
            }
            for(auto it=mp.begin();it!=mp.end();it++){
                sum-=it->second;
                k+=it->second*sum;
            }
            mp.clear();
        }
        return k;
    }
};